<?php
namespace app\admin\model;
use think\Config;
use think\Db;
class BoardModel extends \app\common\model\CommonModel
{
    
    /**
     * $method 总用户数:user_count 品牌总数:brand_count 产品总数:product_count 视频总数:video_count 视频前测次数:video_pre_count
     */
    public function info($data)
    {
        try {
            switch ($data['method']) {
                case 'user_count':
                    $res = Db::table('user')->count();
                    break;
                case 'brand_count':
                    $res = Db::table('brand')->count();
                    break;
                case 'product_count':
                    $res = Db::table('product')->count();
                    break;
                case 'video_count':
                    $res = Db::table('video')->count();
                    break;
                case 'video_pre_count':
                    $res = Db::table('video')->where("status != 'NO_PRETEST'")->count();
                    break;
                default:
                    throw new \Exception("参数错误");
                    break;
            }
            return ['result' => $res];
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }

    /**
     * 下拉列表选项
     * @param string    $data['method']
     */
    public function optionsList($data)
    {
        try {
            switch ($data['method']) {
                case 'username':
                    //用户
                    $sql = "SELECT username,userid FROM user WHERE 1=1 ";
                    if(!empty($params['exact'])){
                        $sql .= "AND username LIKE '%".$params['exact']."%'";
                    }
                    $order = "ORDER BY CONVERT(username USING gbk) COLLATE gbk_chinese_ci ASC";
                    $sql .= $order;
                    $res = $this->getRes($sql); 
                    break;
                case 'brand':
                    //品牌
                    $sql = "SELECT id,brand FROM brand WHERE 1=1 ";
                    if(!empty($params['exact'])){
                        $sql .= "AND brand LIKE '%".$params['exact']."%'";    
                    }
                    $order = "ORDER BY CONVERT(brand USING gbk) COLLATE gbk_chinese_ci ASC";
                    $sql .= $order;
                    $res = $this->getRes($sql);
                    if($res){
                        foreach ($res as $key => $value) {
                            $res[$key]['products'] = Db::table('product')->where(['brand_id' => $value['id']])->select();
                        }
                    } 
                    break;
                case 'product':
                    //产品
                    $sql = "SELECT a.id product_id,a.brand_id,a.product_name,a.advertiser_id,b.brand FROM product a 
                            LEFT JOIN brand b ON a.brand_id = b.id WHERE 1=1 ";
                    if(!empty($params['exact'])){
                        $sql .= "AND product_name LIKE '%".$params['exact']."%'";    
                    }
                    $order = "ORDER BY CONVERT(product_name USING gbk) COLLATE gbk_chinese_ci ASC";
                    $sql .= $order;
                    $res = $this->getRes($sql);
                    break;
                default:
                    throw new \Exception("method参数错误!");
                    break;
            }
            return $res;    
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }
    }
}